package org.jeecg.modules.demo.hd.job;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.hd.service.IAppUserCashDetailService;
import org.jeecg.modules.demo.hd.service.IAppUserCashMainService;
import org.jeecg.modules.demo.hd.service.IAppUserGoldDetailService;
import org.jeecg.modules.demo.hd.service.IAppUserGoldMainService;
import org.jeecg.modules.demo.hd.vo.myProject.UserCashDetailVo;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 平台结算现金
 * <p>
 * 把
 */
@Slf4j
public class SettlementTaskJob implements Job {


    @Resource
    private IAppUserGoldDetailService userGoldDetailService;

    @Resource
    private IAppUserGoldMainService userGoldMainService;

    @Resource
    private ISysUserService userService;
    @Resource
    private IAppUserCashMainService userCashMainService;

    @Resource
    private IAppUserCashDetailService userCashDetailService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //查询所有用户ID
        List<SysUser> list = userService.list();
        // 获取用户 上一天获得的金币（ 广告金币、 团队奖励金币）总和
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String day = localDateTime.format(formatter);
        for (SysUser sysUser : list) {
            Double userGoldNum = userGoldDetailService.quereUserGoldTotalByDayForSettlement(day, sysUser.getId());
            if (ObjectUtils.isNull(userGoldNum) || userGoldNum <= 0) {
                continue;
            }
            // 获得用户 金币剩余总和
            double userGold = userGoldMainService.queryUserGold(sysUser.getId());
            // 对比
            if (userGold < userGoldNum) {
                // 用户金币不够扣除金额,只扣除用户金币余额
                userGoldNum = userGold;
            }
            double rateNum = 0.00001;
            // 计算兑换现金值
            double cashNum = userGoldNum * rateNum;
            //扣除用户金币总量
            userGoldDetailService.exchangeCash(sysUser.getId(), -userGoldNum);
            // 更新用户前一天的 金币明细状态为2
            userGoldDetailService.updateGoldState(sysUser.getId(), day);
            // 兑换现金
            //增加用户现金数量
            UserCashDetailVo cashDetailVo = new UserCashDetailVo();
            cashDetailVo.setUserId(sysUser.getId());
            cashDetailVo.setCashNum(cashNum);
            cashDetailVo.setCashStatus("1");
            cashDetailVo.setName("金币兑换");
            cashDetailVo.setCashType("1");
            userCashDetailService.addDetail(cashDetailVo);
            // 更新用户现金总表
            userCashMainService.updateCashMain(sysUser.getId(), cashNum);
        }
    }
}
